package lcspm.commands;

import java.util.List;
import java.util.Random;
import java.util.Set;

import lcspm.lib.ConfigHandler;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.common.ConfigCategory;

public class CommandWarp
  implements ICommand
{
  public int compareTo(Object arg0)
  {
    return 0;
  }

  public String getCommandName()
  {
    return "warp";
  }

  public String getCommandUsage(ICommandSender icommandsender)
  {
    return "/" + getCommandName() + " list|<name> <IncludePublicWarps>";
  }

  public List getCommandAliases()
  {
    return null;
  }

  public void processCommand(ICommandSender icommandsender, String[] astring)
  {
    if ((icommandsender instanceof EntityPlayer))
    {
      EntityPlayer player = (EntityPlayer)icommandsender;

      if (astring.length == 0) {
        player.addChatMessage("This command requires a warp name");
        return;
      }

      String world = player.worldObj.getWorldInfo().getWorldName();
      String warpName = astring[0];

      if (warpName.equals("list"))
      {
        Set sList = ConfigHandler.getCategories();

        if (sList.size() == 0) {
          player.addChatMessage("There are no warps available.");
        }
        else
        {
          boolean includePublic = false;

          switch (astring.length) {
          case 2:
            includePublic = Boolean.parseBoolean(astring[1]);
          }

          Object[] aList = sList.toArray();
          String vList = "";

          for (int i = 0; i <= aList.length - 1; i++)
          {
            ConfigCategory cc = ConfigHandler.getCategory(aList[i].toString());

            if (cc.get("world").getString().equals(world))
            {
              if (cc.get("player").getString().equals(player.username)) {
                vList = vList + aList[i].toString() + ",";
              }
              else if ((includePublic) && 
                (cc.get("ispublic").getBoolean(false) == includePublic)) {
                vList = vList + aList[i].toString() + ",";
              }

            }

          }

          if (includePublic)
          {
            if (vList.length() == 0) {
              player.addChatMessage("There are no public warps available.");
              return;
            }

            player.addChatMessage(EnumChatFormatting.YELLOW + "All Available Warps:");
          }
          else
          {
            if (vList.length() == 0) {
              player.addChatMessage(EnumChatFormatting.YELLOW + "You have no personal warps available.");
              return;
            }

            player.addChatMessage(EnumChatFormatting.YELLOW + "My Available Warps:");
          }

          player.addChatMessage(EnumChatFormatting.YELLOW + vList.substring(0, vList.length() - 1));
        }

        return;
      }

      if (warpName.equals("home")) {
        ChunkCoordinates spawn = player.getBedLocation(0);

        if (spawn != null) {
          player.addChatMessage("Warp to " + EnumChatFormatting.GREEN + warpName);

          if (player.dimension != 0) {
            player.travelToDimension(0);
          }

          player.setPositionAndUpdate(spawn.posX, spawn.posY, spawn.posZ);
          // Warp sound played
          player.worldObj.playSoundAtEntity(player, "lcspm:warp", 1.0F, 1.0F);
          return;
        }

        player.addChatMessage("A bed is required for this command work.");
        return;
      }

      if (!ConfigHandler.HasCategory(warpName)) {
        player.addChatMessage("The requested warp cannot be found.");

        return;
      }

      ConfigCategory cc = ConfigHandler.getCategory(warpName);

      if (!cc.get("world").getString().equals(world)) {
        player.addChatMessage("The requested warp does not work in this world");
        return;
      }

      if (!cc.get("player").getString().equals(player.username))
      {
        if (!cc.get("ispublic").getBoolean(false)) {
          player.addChatMessage("The requested warp does not exist or belong to you.");
          return;
        }
      }

      lcspm.lib.BackHandler.posX = Double.valueOf(player.posX);
      lcspm.lib.BackHandler.posY = Double.valueOf(player.posY);
      lcspm.lib.BackHandler.posZ = Double.valueOf(player.posZ);
      lcspm.lib.BackHandler.dim = player.dimension;

      player.addChatMessage("Warp to " + EnumChatFormatting.GREEN + warpName);

      String[] pos = cc.get("coor").getString().split(",");

      int dim = 0;

      if (cc.containsKey("dim")) {
        dim = cc.get("dim").getInt();
      }

      if (player.dimension != dim) {
        player.travelToDimension(dim);
      }
      
      // Moves the player to the warp position
      player.setPositionAndUpdate(Double.parseDouble(pos[0]), Double.parseDouble(pos[1]), Double.parseDouble(pos[2]));
      
      // Warp sound played
      player.worldObj.playSoundAtEntity(player, "lcspm:warp", 1.0F, 1.0F);
      
      // Life removal setup
      // peaceful = .25
      // easy = .25
      // normal = .5
      // hard = 1
      
      int ds = player.worldObj.difficultySetting;
      
      float hr = 0;
      switch (ds) {
      case 0:
      case 1:
    	  hr = (float) .25;
    	  break;
      case 2:
    	  hr = (float) .5;
    	  break;
      case 3:
    	  hr = (float) 1;
    	  break;
      }
      
      float pLife = player.func_110143_aJ();
      pLife = (float) (pLife - hr);
      player.setEntityHealth(pLife);
 
    }
  }

  public boolean canCommandSenderUseCommand(ICommandSender icommandsender)
  {
    return true;
  }

  public List addTabCompletionOptions(ICommandSender icommandsender, String[] astring)
  {
    return null;
  }

  public boolean isUsernameIndex(String[] astring, int i)
  {
    return false;
  }
}